﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using SelfService.Model;
using WalkingTec.Mvvm.Core;
using SelfService.Model.Models;

namespace SelfService.ViewModel.SysConfigVMs
{
    public partial class SysConfigVm : BaseCRUDVM<SysConfig>
    {

        public SysConfigVm()
        {
            SetInclude(x => x.Group);
        }

        protected override void InitVM()
        {
        }

        public override void Validate()
        {
            if (DC.Set<SysConfig>().Any(e => e.Key.Trim() == Entity.Key && e.GroupId == Entity.GroupId && e.ID != Entity.ID))
            {
                MSD.AddModelError("Entity.Key", "名称和分类已存在, 请勿重复添加");
                MSD.AddModelError("Entity.GroupId", "名称和分类已存在, 请勿重复添加");
            }
            switch (Entity.ValueType)
            {
                case SysConfigValueTypeEnum.布尔型:
                    if (Entity.Value is not ("true" or "false"))
                    {
                        this.MSD.AddModelError(" ", "值和类型不匹配");
                    }
                    break;
                case SysConfigValueTypeEnum.整数:
                    {
                        if (!int.TryParse(Entity.Value, out var number) || number.ToString() != Entity.Value)
                        {
                            this.MSD.AddModelError(" ", "值和类型不匹配");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.小数:
                    {
                        if (!decimal.TryParse(Entity.Value, out var number) || number.ToString(CultureInfo.InvariantCulture) != Entity.Value)
                        {
                            this.MSD.AddModelError(" ", "值和类型不匹配");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.日期:
                    {
                        if (!DateTime.TryParse(Entity.Value, out var time))
                        {
                            this.MSD.AddModelError(" ", "请输入正确的日期");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.年:
                    {
                        if (!int.TryParse(Entity.Value, out var number) || number is > 9999 or < 1)
                        {
                            this.MSD.AddModelError(" ", "请输入正确的年份");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.月:
                    {
                        if (!int.TryParse(Entity.Value, out var number) || number is > 12 or < 1)
                        {
                            this.MSD.AddModelError(" ", "请输入正确的月份");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.日:
                    {
                        if (!int.TryParse(Entity.Value, out var number) || number is > 31 or < 1)
                        {
                            this.MSD.AddModelError(" ", "请输入正确的日期");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.小时:
                case SysConfigValueTypeEnum.分钟:
                case SysConfigValueTypeEnum.秒:
                    {
                        if (!int.TryParse(Entity.Value, out var number) || number is > 60 or < 1)
                        {
                            this.MSD.AddModelError(" ", "请输入正确的时间格式");
                        }
                    }
                    break;
                case SysConfigValueTypeEnum.字符串:
                    break;
                case SysConfigValueTypeEnum.文件:
                    if (!Guid.TryParse(Entity.Value, out _))
                    {
                        this.MSD.AddModelError(" ", "请上传文件");
                    }
                    break;
                default:
                    this.MSD.AddModelError(" ", "值类型错误");
                    break;
            }
        }

        public override void DoAdd()
        {
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
